﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;
using System.Runtime.InteropServices;

namespace TEdit
{
    public partial class FolderForm : DockContent
    {
        public FolderForm()
        {
            InitializeComponent();
        }

        string selectedPath = "";
        public string SelectedPath
        {
            get
            {
                return this.selectedPath;
            }
            set
            {
                this.selectedPath = value;
                this.Invalidate();
            }
        }

        TreeNode node;
        TreeNode TreeNodeMyComputer;

        private void FolderForm_Load(object sender, EventArgs e)
        {
            GetDirectory();
            if (Directory.Exists(selectedPath))
                setCurrentPath(selectedPath);
            else
                setCurrentPath("home");
        }

        public void GetDirectory()
        {
            tvwMain.Nodes.Clear();

            TreeNode nodemyC;
            nodemyC = new TreeNode();
            nodemyC.Tag = "My Computer";
            nodemyC.Text = "My Computer";
            nodemyC.ImageIndex = 12;
            nodemyC.SelectedImageIndex = 12;
            nodemyC.EnsureVisible();

            TreeNodeMyComputer = nodemyC;
            tvwMain.Nodes.Add(nodemyC);

            ExploreMyComputer();
        }

        private void ExploreTreeNode(TreeNode n)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                //get dirs
                FillFilesandDirs(n);

                //get dirs one more level deep in current dir so user can see there is
                //more dirs underneath current dir
                foreach (TreeNode node in n.Nodes)
                    FillFilesandDirs(node);
            }
            catch
            { }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void GetDirectories(TreeNode parentNode)
        {
            // added after suggestion
            string[] dirList;

            dirList = Directory.GetDirectories(parentNode.Tag.ToString());
            Array.Sort(dirList);

            //check if dir already exists in case click same dir twice
            if (dirList.Length == parentNode.Nodes.Count)
                return;
            //add each dir in selected dir
            for (int i = 0; i < dirList.Length; i++)
            {
                node = new TreeNode();
                node.Tag = dirList[i]; //store path in tag
                node.Text = dirList[i].Substring(dirList[i].LastIndexOf(@"\") + 1);
                node.ImageIndex = 1;
                parentNode.Nodes.Add(node);
            }
        }


        private void FillFilesandDirs(TreeNode comunalNode)
        {
            try
            {
                GetDirectories(comunalNode);
            }
            catch (Exception)
            {
                return;
            }
        }

        public void setCurrentPath(string strPath)
        {
            SelectedPath = strPath;

            if (String.Compare(strPath, "home") == 0)
            {
                txtPath.Text = Application.StartupPath;
            }
            else
            {
                DirectoryInfo inf = new DirectoryInfo(strPath);
                if (inf.Exists)
                {
                    txtPath.Text = strPath;
                }
                else
                    txtPath.Text = Application.StartupPath;
            }
        }

        private void tvwMain_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            string[] drives = Environment.GetLogicalDrives();
            string dir2 = "";

            Cursor.Current = Cursors.WaitCursor;
            TreeNode n;
            TreeNode nodemyC;
            TreeNode nodeDrive;
            nodemyC = e.Node;

            n = e.Node;

            if (n.Text.IndexOf(":", 1) > 0)
            {
                ExploreTreeNode(n);
            }
            else
            {
                if ((String.Compare(n.Text, "My Computer") == 0) && (nodemyC.GetNodeCount(true) < 2))   //add each drive and files and dirs
                {
                    nodemyC.FirstNode.Remove();

                    foreach (string drive in drives)
                    {

                        nodeDrive = new TreeNode();
                        nodeDrive.Tag = drive;

                        nodeDrive.Text = drive;
                        nodeDrive.ImageIndex = 0;
                        nodeDrive.SelectedImageIndex = 0;
                        nodemyC.Nodes.Add(nodeDrive);
                        nodeDrive.EnsureVisible();
                        tvwMain.Refresh();
                        try
                        {
                            //add dirs under drive
                            if (Directory.Exists(drive))
                            {
                                foreach (string dir in Directory.GetDirectories(drive))
                                {
                                    dir2 = dir;
                                    node = new TreeNode();
                                    node.Tag = dir;
                                    node.Text = dir.Substring(dir.LastIndexOf(@"\") + 1);
                                    node.ImageIndex = 1;
                                    nodeDrive.Nodes.Add(node);
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                        nodemyC.Expand();
                    }
                }

                else
                {
                    ExploreTreeNode(n);
                }
            }
            Cursor.Current = Cursors.Default;
        }

        private void tvwMain_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            TreeNode n;
            n = e.Node;

            try
            {
                if (String.Compare(n.Text, "My Computer") == 0)
                {
                }
                else
                {
                    txtPath.Text = n.Tag.ToString();
                    fileListBox1.Path = txtPath.Text;
                }
            }
            catch { }
        }

        private void tvwMain_DoubleClick(object sender, System.EventArgs e)
        {
            if (!tvwMain.SelectedNode.IsExpanded)
                tvwMain.SelectedNode.Collapse();
            else
            {
                ExploreTreeNode(tvwMain.SelectedNode);
            }
        }

        private void ExploreMyComputer()
        {
            string[] drives = Environment.GetLogicalDrives();

            Cursor.Current = Cursors.WaitCursor;
            TreeNode nodeDrive;

            if (TreeNodeMyComputer.GetNodeCount(true) < 2)
            {
                //TreeNodeMyComputer.FirstNode.Remove();

                foreach (string drive in drives)
                {
                    nodeDrive = new TreeNode();
                    nodeDrive.Tag = drive;

                    nodeDrive.Text = drive;
                    nodeDrive.ImageIndex = 8;
                    nodeDrive.SelectedImageIndex = 8;


                    TreeNodeMyComputer.Nodes.Add(nodeDrive);
                    try
                    {
                        //add dirs under drive
                        if (Directory.Exists(drive))
                        {
                            foreach (string dir in Directory.GetDirectories(drive))
                            {
                                node = new TreeNode();
                                node.Tag = dir;
                                node.Text = dir.Substring(dir.LastIndexOf(@"\") + 1);
                                node.ImageIndex = 1;
                                nodeDrive.Nodes.Add(node);
                            }
                        }


                    }
                    catch (Exception ex)	//error just add blank dir
                    {
                        MessageBox.Show("Error while Filling the Explorer:" + ex.Message);
                    }
                }
            }

            TreeNodeMyComputer.Expand();
        }
    }
}
